//
//  MResetPwdViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/11/7.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MResetPwdViewController.h"
#import "MVIPCardService.h"
#import "MDDataBaseUtil.h"
#import "MViewUtil.h"
@interface MResetPwdViewController ()<UIAlertViewDelegate> {

    MViewUtil *viewUtil;
}

- (IBAction)backAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UITextField *oldPwdTxt;

@property (weak, nonatomic) IBOutlet UITextField *PwdTxt;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwdTxt;
@property (weak, nonatomic) IBOutlet UIButton *okBtn;
- (IBAction)okBtnAction:(UIButton *)sender;


@end

@implementation MResetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self _initView];
}
- (void)_initView {

    [self.contentView.layer setCornerRadius:5.0];
    [self.contentView.layer setMasksToBounds:YES];
    [self.okBtn.layer setCornerRadius:3.0];
    [self.okBtn.layer setMasksToBounds:YES];
    
    [self.okBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.okBtn.layer setBorderWidth:1.0];
    viewUtil = [[MViewUtil alloc]init];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backAction:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)showAlertViewWithTitle:(NSString *)title tag:(NSInteger)tag{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert setTag:tag];
    [alert show];
}
- (IBAction)okBtnAction:(UIButton *)sender {
    
    if ([self.oldPwdTxt.text isEqualToString:@""]) {
        [self showAlertViewWithTitle:@"请输入原密码" tag:0];
    } else {
    
        if ([self.PwdTxt.text isEqualToString:@""]) {
            [self showAlertViewWithTitle:@"请输入新的操作密码" tag:1];
        } else {
        
            if ([self.confirmPwdTxt.text isEqualToString:@""]) {
                [self showAlertViewWithTitle:@"请再次输入操作密码" tag:2];
            } else {
            
                if (![self.PwdTxt.text isEqualToString:self.confirmPwdTxt.text]) {
                    [self showAlertViewWithTitle:@"密码不匹配" tag:3];
                }else {
                
                    [[MVIPCardService sharedClient]getVipCardChangePwdWithType:@"1" userID:[[MDDataBaseUtil userID]integerValue] oldPassword:self.oldPwdTxt.text newPassword:self.PwdTxt.text completion:^(NSString *result){
                    
                        if (result) {
                            // 8888
                            if ([result isEqualToString:@"成功修改密码"]) {
                                [self showAlertViewWithTitle:result tag:4];
                            }else {
                            
                                [self showAlertViewWithTitle:result tag:5];
                            }
                            
                        }
                    } failure:^(NSString *error){
                    
                        [viewUtil showShortDurationMessage:error targetView:self.view];
                    }];
                }
            }
            
        }
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {


    if (alertView.tag == 4) {
        if (buttonIndex == 0) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}
@end
